package com.yita.demo04_project_reflect.dao.Impl;

import com.yita.demo04_project_reflect.dao.FruitDAO;
import com.yita.demo04_project_reflect.dao.Impl.base.BaseDAO;
import com.yita.demo04_project_reflect.pojo.Fruit;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {


    @Override
    public List<Fruit> getFruitList() {
        String sql = "select * from t_fruit";
        return executeQuery(sql);
    }

    @Override
    public boolean addFruit(Fruit fruit) {
        String sql = "insert into t_fruit values (0,?,?,?,?)";

        return executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark()) > 0;
    }

    @Override
    public boolean updateFruit(Fruit fruit) {

        String sql = "update t_fruit set fcount=? where fid=?";

        return executeUpdate(sql, fruit.getFcount(), fruit.getFid()) > 0;
    }

    @Override
    public Fruit getFruitByFname(String fname) {

        String sql = "select * from t_fruit where fname=? limit 0,1";
        List<Fruit> fruits = executeQuery(sql, fname);
        return fruits.size() > 0 ? fruits.get(0) : null;
    }

    @Override
    public boolean delFruit(String fname) {
        String sql = "delete from t_fruit where fname=?";
        return executeUpdate(sql, fname) > 0;
    }
}
